//
//  STCalendarKitWeekManger.m
//  ZuoBiao
//
//  Created by stoneobs on 2017/7/29.
//  Copyright © 2017年 shixinyun. All rights reserved.
//

#import "STCalendarKitWeekManger.h"
#import "NSDate+STFormatter.h"
#import "STCalendarKit.h"
@implementation STCalendarKitWeekManger
+ (STCalendarKitWeekManger *)defult{

    static STCalendarKitWeekManger * defult = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        defult = [STCalendarKitWeekManger new];
    });
    return defult;
}
- (NSArray<STCalendarKitDayModel *> *)calculateNextWeeksWithDate:(NSDate *)date nums:(NSInteger)nums{
    
    NSMutableArray * array = [NSMutableArray new];
    //date 所在的那周 周一的date
    NSDate * weekFristDate = [NSCalendar dateForWeekFirstDay:date];
    //添加这周的数据
    for (int i = 0; i < 7; i ++) {
        STCalendarKitDayModel * model = [STCalendarKitDayModel new];
        model.date = weekFristDate;
        model.isThisMonth = YES;
        model.dayValue = [[weekFristDate.yyyyMMddByLineWithDate componentsSeparatedByString:@"-"].lastObject intValue];
        if ([model.date.yyyyMMddByLineWithDate isEqualToString:[NSDate localCurrentDate].yyyyMMddByLineWithDate]) {
            model.isToday = YES;
        }
        [array addObject:model];
        weekFristDate = [NSCalendar nextDayDate:weekFristDate];
    }
    //这周的最后一天
    NSDate * weekLastDate = [NSCalendar dateForWeekLastDay:date];
    //下一周的第一天
    NSDate * nextWeekFristDate = [NSCalendar nextDayDate:weekLastDate];
    
    for (int i = 0 ; i < nums * 7; i ++) {
        STCalendarKitDayModel * model = [STCalendarKitDayModel new];
        model.date = nextWeekFristDate;
         model.isThisMonth = YES;
        model.dayValue = [[nextWeekFristDate.yyyyMMddByLineWithDate componentsSeparatedByString:@"-"].lastObject intValue];
        if ([model.date.yyyyMMddByLineWithDate isEqualToString:[NSDate localCurrentDate].yyyyMMddByLineWithDate]) {
            model.isToday = YES;
        }
        [array addObject:model];
        nextWeekFristDate = [NSCalendar nextDayDate:nextWeekFristDate];
    }


    return array;
}
- (NSArray<STCalendarKitDayModel *> *)calculatelastAllWeeksWithDate:(NSDate *)date{
    NSMutableArray * array = [NSMutableArray new];
    //date 所在的那周 周一的date
    NSDate * weekFristDate = [NSCalendar dateForWeekFirstDay:date];
    //日历初始月份,
    NSDateFormatter * formater = [[NSDateFormatter alloc] init];
    formater.dateFormat = @"YYYY-MM-dd";
    NSDate * originDate = [formater dateFromString:STCalendarKitViewBeginDateString].localDate;
    NSDate * cacluDate = [[NSCalendar dateForWeekFirstDay:originDate] copy];
    do {
        STCalendarKitDayModel * model = [STCalendarKitDayModel new];
        model.date = cacluDate;
         model.isThisMonth = YES;
        model.dayValue = [[cacluDate.yyyyMMddByLineWithDate componentsSeparatedByString:@"-"].lastObject intValue];
        if ([model.date.yyyyMMddByLineWithDate isEqualToString:[NSDate localCurrentDate].yyyyMMddByLineWithDate]) {
            model.isToday = YES;
        }
        [array addObject:model];
        
        cacluDate = [NSCalendar nextDayDate:cacluDate];
        
        
    } while (![cacluDate.yyyyMMddByLineWithDate isEqualToString:weekFristDate.yyyyMMddByLineWithDate]);
    
    return array;

}
- (NSArray<STCalendarKitDayModel *> *)calculatelastWeeksWithDate:(NSDate *)date nums:(NSInteger)nums{
    
    NSMutableArray * array = [NSMutableArray new];
    //date 所在的那周 周一的date
    NSDate * weekFristDate = [NSCalendar dateForWeekFirstDay:date];
    //日历初始月份,
    NSDateFormatter * formater = [[NSDateFormatter alloc] init];
    formater.dateFormat = @"YYYY-MM-dd";
    NSDate * originDate = [formater dateFromString:STCalendarKitViewBeginDateString].localDate;
    NSDate * cacluDate = [originDate copy];
    //多少周内 最后一天的数据
    NSDate * lastDayDate = [[NSCalendar lastDayDate:weekFristDate] copy];
    for (int i = 0; i < nums * 7; i++) {
        STCalendarKitDayModel * model = [STCalendarKitDayModel new];
         model.isThisMonth = YES;
        model.date = lastDayDate;
        model.dayValue = [[lastDayDate.yyyyMMddByLineWithDate componentsSeparatedByString:@"-"].lastObject intValue];
        if ([model.date.yyyyMMddByLineWithDate isEqualToString:[NSDate localCurrentDate].yyyyMMddByLineWithDate]) {
            model.isToday = YES;
        }
        [array addObject:model];
        lastDayDate = [NSCalendar lastDayDate:lastDayDate];
    }

    
    return array;

}
@end
